package ibm.cognos.imt;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 3/20/14
 * Time: 10:09 AM
 * To change this template use File | Settings | File Templates.
 */
public class ReportFactory {
    public static GroovyObject createReportFromSpec(String spec) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        ClassLoader parent = GroovyScriptHelper.class.getClassLoader();
        GroovyClassLoader loader = new GroovyClassLoader(parent);
        Class groovyClass = loader.parseClass(new File("app/models/groovy/CogReport.groovy"));
        Constructor[] constructors= groovyClass.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            Class[] types = constructor.getParameterTypes();
            for (Class type : types) {
                System.out.println(type);
            }
        }
        GroovyObject reportInstance = (GroovyObject) groovyClass.getDeclaredConstructor(String.class).newInstance(spec);
        return reportInstance;
    }
}
